//
//  GXContentManagerController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/29.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXContentManagerController.h"
#import "GXContentManagerCell.h"
#import "GXAddContentController.h"
#import "GXBatchManageController.h"
#import "GXGroupManageController.h"
#import "GXShopBrowseController.h"
#import "GXFeatureCommonController.h"
@interface GXContentManagerController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * contentTableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)NSMutableArray * menuList;
@property (nonatomic,copy) NSString * sectionStr;
@end
static NSString * const contentManagerCell=@"GXContentManagerCell";
@implementation GXContentManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"内容管理";
    [self requestData];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.contentTableView];
}
- (void)requestData
{
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [MenuContentManager selectMenuListWithStoreID:storeId success:^(id responseObject) {
        [self handleMessageData:responseObject];
    } fail:^(id responseObject) {
    }];
}
- (void)handleMessageData:(id)responseObj
{
    NSDictionary *contentDic = responseObj[@"result"];
    if (![contentDic[@"services"] count]) {
        [SVProgressHUD showImage:nil status:@"无数据"];
    }else
    {
        self.menuList = [NSMutableArray arrayWithArray:contentDic[@"services"]];
    }
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString * filePath = [[NSBundle mainBundle]pathForResource:@"ContentManagerData" ofType:@"plist"];
        NSMutableArray * array =[[NSMutableArray alloc] initWithContentsOfFile:filePath];
        for (NSDictionary * dict in array) {
            [_dataArray addObject:dict];
        }
    }
    return _dataArray;
}
- (UITableView *)contentTableView
{
    if (_contentTableView == nil) {
        _contentTableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,kUIScreenWidth,kUIScreenHeight-Height_TabBar-Height_StatusBar) style:UITableViewStyleGrouped];
        _contentTableView.delegate=self;
        _contentTableView.dataSource=self;
        _contentTableView.sectionFooterHeight = 0.01f;
        _contentTableView.sectionHeaderHeight= 15;
        _contentTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _contentTableView.showsHorizontalScrollIndicator=NO;
        _contentTableView.showsVerticalScrollIndicator=NO;
        _contentTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _contentTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_contentTableView  registerNib:[UINib nibWithNibName:@"GXContentManagerCell" bundle:nil] forCellReuseIdentifier:contentManagerCell];
    }
    return _contentTableView;
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GXContentManagerCell * cell = [tableView dequeueReusableCellWithIdentifier:contentManagerCell];
    cell.contentDic =self.dataArray[indexPath.section];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==0) {
        [self jumpController:[GXAddContentController class]];
        //新添内容
    }else if (indexPath.section ==1)
    {
        [self jumpController:[GXGroupManageController class]];
        //分组管理
    }else if (indexPath.section ==5)
    {
//        [self jumpController:[GXShopBrowseController class]];
        GXShopBrowseController *browseVC = [[GXShopBrowseController alloc]init];
        if (self.menuList.count) {
            browseVC.rightDataSource = [NSArray arrayWithArray:self.menuList];
        }
        [self.navigationController pushViewController:browseVC animated:YES];
        //店铺游览
    }else
    {
        GXFeatureCommonController * featureVC = [[GXFeatureCommonController alloc]init];
        [self.navigationController pushViewController:featureVC animated:YES];
        if (indexPath.section ==2){
            featureVC.titleName = @"轮播图";
        }
        if (indexPath.section ==3)
        {
            featureVC.titleName = @"特色菜品";
        }
        if (indexPath.section ==4)
        {
            featureVC.titleName = @"商家特色";
            //商家特色
        }
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footerView = [[UIView alloc]init];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}
-(void)jumpController:(Class)VC
{
    UIViewController * controller = [[VC alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
@end
